window.onload = function () {
    let arrow_l = document.querySelector('.arrow-l');
    let arrow_r = document.querySelector('.arrow-r');
    let focus = document.querySelector('.focus');
    let ul = focus.querySelector('ul');
    let ol = focus.querySelector('ol');
    let focusWidth = focus.offsetWidth;
    let num = 0; // 点击右侧按钮，图片滚动
    let circle = 0; // 点击右侧按钮，让底部圆点加1，控制底部圆圈的播放
    let timer = null;
    let flag = true; // 节流
    console.log(focusWidth);
    focus.addEventListener('mouseenter', function () {
        arrow_l.style.display = 'block';
        arrow_r.style.display = 'block';
        clearInterval(timer);
        timer = null;
    });
    focus.addEventListener('mouseleave', function () {
        arrow_l.style.display = 'none';
        arrow_r.style.display = 'none';
        timer = setInterval(() => {
            arrow_r.click();
        }, 2000)
    });
    for(let i = 0; i < ul.children.length; i++) {
        let li = document.createElement('li');
        li.setAttribute('index', i);
        ol.appendChild(li);
        li.onclick = function () {
            for(let i = 0; i < ol.children.length; i++) {
                ol.children[i].className = '';
            }
            this.className = 'current';
            let index = this.getAttribute('index');
            circle = index;
            num = index;
            animate(ul, -index * focusWidth, function () {})
        }
    }
    ol.children[0].className = 'current';
    // 克隆图片实现无缝滚动
    let first = ul.children[0].cloneNode(true);
    ul.appendChild(first);

    // 图片点击
    arrow_r.addEventListener('click', function () {
        if(flag) {
            flag = false;
            if(num === ul.children.length - 1) {
                ul.style.left = 0;
                num = 0;
            }
            num++;

            animate(ul, -num * focusWidth, function () {
                flag = true;
            });
            circle++;
            if(circle === ol.children.length) {
                circle = 0;
            }
            for(let i = 0; i < ol.children.length; i++) {
                ol.children[i].className = '';
            }
            ol.children[circle].className = 'current'
        }
    });

    arrow_l.addEventListener('click', function () {
        if(flag) {
            flag = false;
            if(num === 0) {
                num = ul.children.length - 1;
                ul.style.left = -num * focusWidth + 'px';
            }
            num--;

            animate(ul, -num * focusWidth, function () {
                flag = true;
            });
            circle--;
            if(circle < 0) {
                circle = ol.children.length - 1;
            }
            for(let i = 0; i < ol.children.length; i++) {
                ol.children[i].className = '';
            }
            ol.children[circle].className = 'current';
        }
    });
    timer = setInterval(() => {
        arrow_r.click();
    }, 2000)
}
